#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

#define EXEC_FILE "./raytrace"

int main(int argc, char *argv[]) {
    char str[100];
    int num, i;
    int status;

    if (argc != 2) {
        fprintf(stderr, "Usage: run [number]\n");
        exit(EXIT_FAILURE);
    }

    num = atoi(argv[1]);

    if (fork()) {
        wait(&status);
        if (status) {
            fprintf(stderr, "make failed\n");
            exit(EXIT_FAILURE);
        }
    }
    else {
        execlp("make", "make", (char *) NULL);
    }

    for (i = 0; i < num; i++) {
        snprintf(str, 100, "sim/sim%d.sim", i);

        if (fork()) {
            wait(&status);
            if (status != 0) {
                fprintf(stderr, "failure: %d\n", status);
                exit(EXIT_FAILURE);
            }
        }
        else {
            execlp(EXEC_FILE, "./raytrace", "800", "600", "-I", 
             "povray/sim.pov", "-a", "1", "-r", "3", "-w",
             str, (char *) NULL);
        }
    }

    return 0;
}
